import regeneratorRuntime from '../../lib/runtime/runtime'

import { request } from "../../request/index"

// pages/goods_list/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {
        id: 0,
        value: '综合',
        isActive: true
      },
      {
        id: 1,
        value: '销量',
        isActive: false
      },
      {
        id: 2,
        value: '价格',
        isActive: false
      },
    ],
    goodsList: [],
  },

  // 接口要的参数
  QueryParams: {
    query: "",
    cid: "",
    pagenum: 1,
    pagesize: 50
  },

  // 总页数
  totalPages:1,

  onLoad: function (options) {
    // console.log(options);
    this.QueryParams.cid = options.cid;
    this.getGoodsList();
  },

  // 获取商品列表数据
  async getGoodsList() {
    const res = await request({ url: "/goods/search", data:this.QueryParams });
    console.log(res);
    const total = res.data.message.total;
    this.totalPages = Math.ceil(total / this.QueryParams.pagesize)
    console.log(this.totalPages);
    this.setData({
      goodsList: res.data.message,
      // goodsList: [...this.data.goodsList,...res.data.message.goods]
      // goodsList: this.data.goodsList.concat(res.data.message.goods)
    })
    console.log(this.data.goodsList);
  },

  // 商品分类页面标题点击， 从子组件(Tabs)传递过来
  handleTabsItemChange(e) {
    // console.log(e);
    // 1、获取被点击的标题索引
    const { index } = e.detail;
    // 2、修改原数组
    let { tabs } = this.data;
    tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false);
    // 3、赋值到data中
    this.setData({
      tabs
    })
  },

  // 页面赏花 滚动条触底事件
  onReachBottom() {
    if (this.QueryParams.pagenum >= this.totalPages) {
      wx.showToast({
        title: '无了',
      });
    } else {
      console.log('you');
      this.QueryParams.pagenum++;
      this.getGoodsList();
    }
  }
})